package com.jeecms.wicool.action.car;

import static com.jeecms.common.page.SimplePage.cpn;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.manager.main.CmsLogMng;
import com.jeecms.cms.web.CmsUtils;
import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.car.CarBrand;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.car.CarBrandMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;

@Controller
public class CarBrandAct {
	private static final Logger log = LoggerFactory.getLogger(CarBrandAct.class);

	@RequestMapping("/carBrand/v_list.do")
	public String list(String brandName,Integer pageNo,HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage(brandName,cpn(pageNo),
				CookieUtils.getPageSize(request),CmsUtils.getUser(request));
		model.addAttribute("pagination", pagination);
		model.addAttribute("brandName", brandName);
		return "car/list_carBrand";
	}
	
	@RequestMapping("/carBrand/v_edit.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		CarBrand carBrand = manager.findById(id);
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("carBrand",carBrand);
		model.addAttribute("listShop",listShop);
		return "car/edit_carBrand";
	}
	@RequestMapping("/carBrand/v_view.do")
	public String view(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		CarBrand carBrand = manager.findById(id);
		model.addAttribute("carBrand",carBrand);
		return "car/view_carBrand";
	}
	@RequestMapping("/carBrand/o_save.do")
	public String save(CarBrand bean, Integer shopId,HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(bean, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = manager.save(bean,shopId);
		log.info("save CarBrand id={}", bean.getId());
		cmsLogMng.operating(request, "CarBrand.log.save",  "id=" + bean.getId());
		return "redirect:v_list.do";
	}
	@RequestMapping("/carBrand/o_update.do")
	public String update(CarBrand bean,Integer shopId,Integer pageNo,HttpServletRequest request,ModelMap model) {
		WebErrors errors = validateUpdate(bean.getId(), request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		SimpleDateFormat simpleTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateTime = simpleTime.format(new Date());
		Timestamp t = Timestamp.valueOf(dateTime);

		bean.setUpdateTime(t);
		bean=manager.update(bean,shopId);
		model.addAttribute("message", "global.success");
		log.info("update CarBrand success. id={}", bean.getId());
		cmsLogMng.operating(request, "CarBrand.log.update",  "id=" + bean.getId());
		return list(null,pageNo,request, model);
	}
	
	@RequestMapping("/carBrand/v_add.do")
	public String add(ModelMap model,HttpServletRequest request) {
		CarBrand carBrand = new CarBrand();
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("carBrand", carBrand);
		model.addAttribute("listShop", listShop);
		return "car/add_carBrand";
	}
	

	@RequestMapping("/carBrand/o_delete.do")
	public String delete(Integer[] ids, String brandName,Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		WebErrors errors = validateDelete(ids, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		CarBrand[] beans = manager.deleteByIds(ids);
		for (CarBrand bean : beans) {
			log.info("delete CarBrand id={}", bean.getId());
			cmsLogMng.operating(request, "CarBrand.log.delete", "id="
					+ bean.getId() + ";name=" + bean.getBrandName());
		}
		return list(brandName,pageNo, request, model);
	}
	private WebErrors validateSave(CarBrand bean, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		return errors;
	}

	private WebErrors validateEdit(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateUpdate(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		// TODO 验证是否为管理员，管理员不允许修改。
		return errors;
	}
	private WebErrors validateDelete(Integer[] ids, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		return errors;
	}

	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		CarBrand entity = manager.findById(id);
		if (errors.ifNotExist(entity, CarBrand.class, id)) {
			return true;
		}
		return false;
	}
	@Autowired
	private CarBrandMng manager;
	@Autowired
	private WiShopInfoMng shopMng;
	@Autowired
	private CmsLogMng cmsLogMng;
}
